<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>实体服务测试</title>
</head>
<body>
    <form action="" id="form">
        <input type="text" name="id" placeholder="id：用于测试使用"/>
        <br>
        <input type="text" name="username" placeholder="姓名"/>
        <br>
        <input type="text" name="nikename" placeholder="昵称"/>
        <br>
        <input type="button" value="form提交" onclick="submission()"/>
        <input type="button" value="json提交" onclick="submission2()"/>
        <input type="button" value="form修改" onclick="submission3()"/>
    </form>

    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
         //form表单转json传递参数 - 新增
         function submission() {
             var data = $("#form").serializeArray();
             data = convertToJson(data);
             $.ajax({
                 url: '/crud/entity/insertReCount/User',
                 type: 'POST',
                 dataType: "json",
                 // 指定这个协议很重要
                 contentType: "application/json;charset=UTF-8",
                 // json对象转成字符串
                 data: JSON.stringify(data),
                 success: function (data) {
                     console.info(JSON.stringify(data))
                 }
             });
         }

         //提取所有input转json传递 - 新增
         function submission2() {
             var data = {
                 username:$("input[name='username']").val(),
                 nikename:$("input[name='nikename']").val()
             };
             $.ajax({
                 url: '/crud/entity/insertReCount/User',
                 type: 'POST',
                 dataType: "json",
                 // 指定这个协议很重要
                 contentType: "application/json;charset=UTF-8",
                 // json对象转成字符串
                 data: JSON.stringify(data),
                 success: function (data) {
                     console.info(JSON.stringify(data))
                 }
             });
         }

         //form表单转json传递参数 - 修改
         function submission3() {
             var data = $("#form").serializeArray();
             data = convertToJson(data);
             $.ajax({
                 url: '/crud/entity/updateReCount/User',
                 type: 'POST',
                 dataType: "json",
                 // 指定这个协议很重要
                 contentType: "application/json;charset=UTF-8",
                 // json对象转成字符串
                 data: JSON.stringify(data),
                 success: function (data) {
                     console.info(JSON.stringify(data))
                 }
             });
         }






         /**
          * 将表单对象转为json对象
          * @param formValues
          * @returns
          */
         function convertToJson(formValues) {
             var result = {};
             for(var formValue,j=0;j<formValues.length;j++) {
                 formValue = formValues[j];
                 var name = formValue.name;
                 var value = formValue.value;
                 if (name.indexOf('.') < 0) {
                     if(result[name]) {
                         result[name] = result[name] + "," + value;
                     } else {
                         result[name] = value;
                     }
                     continue;
                 } else {
                     var simpleNames = name.split('.');
                     // 构建命名空间
                     var obj = result;
                     for ( var i = 0; i < simpleNames.length - 1; i++) {
                         var simpleName = simpleNames[i];
                         if (simpleName.indexOf('[') < 0) {
                             if (obj[simpleName] == null) {
                                 obj[simpleName] = {};
                             }
                             obj = obj[simpleName];
                         } else { // 数组
                             // 分隔
                             var arrNames = simpleName.split('[');
                             var arrName = arrNames[0];
                             var arrIndex = parseInt(arrNames[1]);
                             if (obj[arrName] == null) {
                                 obj[arrName] = []; // new Array();
                             }
                             obj = obj[arrName];
                             multiChooseArray = result[arrName];
                             if (obj[arrIndex] == null) {
                                 obj[arrIndex] = {}; // new Object();
                             }
                             obj = obj[arrIndex];
                         }
                     }

                     if(obj[simpleNames[simpleNames.length - 1]] ) {
                         var temp = obj[simpleNames[simpleNames.length - 1]];
                         obj[simpleNames[simpleNames.length - 1]] = temp;
                     }else {
                         obj[simpleNames[simpleNames.length - 1]] = value;
                     }

                 }
             }
             return result;
         }
    </script>
</body>
</html>